﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace System.Web.UI.WebControls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:UEditorNet runat=server></{0}:UEditorNet>")]
    public class UEditorNet : TextBox
    {

        public UEditorNet()
        {
        }

        //[Bindable(true)]
        //[Category("Appearance")]
        //[DefaultValue("")]
        //[Localizable(true)]
        //public string Text
        //{
        //    get
        //    {
        //        String s = (String)ViewState["Text"];
        //        return ((s == null) ? String.Empty : s);
        //    }

        //    set
        //    {
        //        ViewState["Text"] = value;
        //    }
        //}


        /*
        <!--style给定宽度可以影响编辑器的最终宽度-->
    <script type="text/javascript">
        // 自定义的编辑器配置项,此处定义的配置项将覆盖editor_config.js中的同名配置
        var editorOption = {
            //这里可以选择自己需要的工具按钮名称,此处仅选择如下五个
            //toolbars: [['FullScreen', 'Source', 'Undo', 'Redo', 'Bold']],
            //focus时自动清空初始化时的内容
            //autoClearinitialContent: true,
            //关闭字数统计
            wordCount: false,
            //关闭elementPath
            elementPathEnabled: false
            //更多其他参数，请参考editor_config.js中的配置项
        };
        var editor_a = new baidu.editor.ui.Editor(editorOption);
        editor_a.render('<%=TextBox1.ClientID %>');
        editor_a.addListener('selectionchange', function () {
            document.getElementById('<%=TextBox1.ClientID %>').value = editor_a.getContent();
        });
    </script>
*/




        protected override void Render(HtmlTextWriter writer)
        {
            this.RenderBeginTag(writer);
            HttpUtility.HtmlEncode(this.Text, writer);
            this.RenderEndTag(writer);
            string ScriptStr = "            ";
            writer.Write("\r\n        ");
            writer.AddAttribute(HtmlTextWriterAttribute.Type, "text/javascript");
            writer.RenderBeginTag(HtmlTextWriterTag.Script);
            writer.Write("    var " + this.ClientID + "_Option = {");
            writer.Write("\r\n");//添加设置的属性
            writer.Write(ScriptStr + "};\r\n");
            writer.Write(ScriptStr + "var " + this.ClientID + "_Object = new baidu.editor.ui.Editor(" + this.ClientID + "_Option);\r\n");
            writer.Write(ScriptStr + this.ClientID + "_Object.render('" + this.ClientID + "');\r\n");
            writer.Write(ScriptStr + this.ClientID + "_Object.addListener('selectionchange', function () {\r\n");
            writer.Write(ScriptStr + "    document.getElementById('" + this.ClientID + "').value = " + this.ClientID + "_Object.getContent();\r\n");
            //writer.Write(ScriptStr + "    Ext.getCmp('" + this.ClientID + "').value = " + this.ClientID + "_Object.getContent();\r\n");
            writer.Write(ScriptStr + "});\r\n");
            writer.Write("        </script>");
            //writer.RenderEndTag();
        }

        
        /// <summary>获取或设置 System.Web.UI.WebControls.TextBox 控件的行为模式（单行、多行或密码）。 <see cref="T:System.Web.UI.WebControls.TextBox" /> control.</summary>
        /// <returns>System.Web.UI.WebControls.TextBoxMode 枚举值之一。默认值为 SingleLine。</returns>
        /// <exception cref="T:System.ArgumentOutOfRangeException">指定的模式不是 System.Web.UI.WebControls.TextBoxMode 枚举值之一。</exception>
        [DefaultValue(TextBoxMode.MultiLine), Themeable(false), Category("Behavior"), Description("TextBox_TextMode")]
        public override TextBoxMode TextMode
        {
            get
            {
                return TextBoxMode.MultiLine;
            }

        }
        

    }
}

